$(function () {


    layui.use('form', function () {
        var form = layui.form;

        form.on('submit(login)', function (data) {

            console.log("登录提交的数据:",data.field);
            var token=getCookie("keji_token")
            $.ajax({
                url: 'http://localhost:11111/api/s/admin/acl/index/login',

                // url: 'http://localhost:8080/back/userLogin/login',//对应的是Java servlet里面的注解地址
                data:JSON.stringify(data.field),// 要传递给Java的数据，统一以对象的形式传递{}
                type: 'post',//对应的是servlet里面的方法
                contentType: 'application/json',
                dataType: 'json',//期待后端返回给前端的数据格式
                xhrFields:{withCredentials:true},
                headers:{"token":"Bearer " +localStorage.getItem("token")},
                // headers:{'token':document.cookie.split("en")[1]},
                // headers:{'token':token},
                crossDomain:true,//是否为跨域请求
                success: function (res,stutas,xhr) {// Java后台给前端的返回信息，res就是返回的结果
                    console.log("res:",res);

                    if(res.data.msg=="password-error"){
                        layer.msg("密码或者用户名错误")
                    }else if(res.data.msg=="code-error"){

                        layer.msg("验证码错误");
                    }else if(res.message =="成功"){
                        layer.msg("登录成功",function () {
                            localStorage.setItem("token",res.data.token)
                            // $.ajax({
                            //     url: 'http://localhost:11111/api/k/back/userLogin/getMemberInfo',
                            //     type:'get',
                            //     dataType: 'json',
                            //     crossDomain:true,//是否为跨域请求
                            //     contentType: 'application/json',
                            //     xhrFields:{withCredentials:true},
                            //     headers:{"Authorization":"Bearer "+localStorage.getItem("token")},
                            //     // headers:{'token':getCookie("keji_token")},
                            //     success: function (res) {
                            //
                            //         console.log("放进去了")
                            //         // setCookie("keji_ucenter",res.data.userInfo)
                            //         // console.log("cookie:",document.cookie)
                            //
                            //     }
                            // });
                            console.log("后台返回数据",res)
                            var tt=res.data.token;
                            console.log("token",tt)
                            // document.cookie="keji_token"+tt
                            setCookie("keji_token",tt)
                            setCookie("keji_ucenter","11")
                            // console.log("cookie:",document.cookie)
                            console.log("cookietoken:",token)
console.log("KEJIT:",getCookie("keji_token"))

                            location.href = "http://localhost:63342/kejifront-master/front_page/html/"
//3js

                        });
                    }
                },
                error: function (res) {
                    console.log("错误返回的数据:",res);
                    alert("后端返回值错误")
                }
            });
            return false;
        });

        form.verify({
            username:[/^[A-z]/,"名字必须以字母开头"],
            password:[/(.+){5,8}/,"密码长度5到8位"],
        });


    });
})

function changeCode() {
    var number = Math.random();
    $("#checkCode").attr("src","http://localhost:8080/back/code?a="+number);
}

function display(obj) {

    //当前确认输入框类型是密码
    if($("#password").attr("type") == "password"){
        $("#password").attr("type","text")
        $(obj).attr("src","../img/open.png")
    }
    //当前确认输入框类型是文本
    else {
        $("#password").attr("type","password")
        $(obj).attr("src","../img/close.png")
    }
}
function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function clearCookie(name) {
    setCookie(name, "", -1);
}
function clearAllCookie() {
    var date=new Date();
    date.setTime(date.getTime()-10000);
    var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
    console.log("需要删除的cookie名字："+keys);
    if (keys) {
        for (var i =  keys.length; i--;)
            document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
    }
}
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
